package com.ply.flashsalessystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ply.flashsalessystem.annotation.AuthorityName;
import com.ply.flashsalessystem.annotation.UserAuthority;
import com.ply.flashsalessystem.entity.pojo.UserAmount;
import com.ply.flashsalessystem.entity.result.Result;
import com.ply.flashsalessystem.entity.status.UserAmountStatus;
import com.ply.flashsalessystem.entity.vo.UserAmountVo;
import com.ply.flashsalessystem.entity.wrapper.UserWrapper;
import com.ply.flashsalessystem.service.UserAmountService;
import com.ply.flashsalessystem.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author XiaoPan
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/userAmount")
@Api("用户账户 接口")
@CrossOrigin
public class UserAmountController {
    //查询账户流水信息
//    分页查询
    @Autowired
    UserAmountService userAmountService;


//    @AuthorityName(authorityId = 61, authorityName = "用户 分页查询账户流水信息")
    @UserAuthority
    @ApiOperation("用户 分页查询账户流水信息")
    @PostMapping(value = {"user/getAmount/{current}/{limit}", "user/getAmount"})
    public Result getAmount(@PathVariable(value = "current", required = false) Integer current,
                            @PathVariable(value = "limit", required = false) Integer limit) {
        if (current == null || limit == null) {
            current = 0;
            limit = 10;
        }
        UserWrapper userWrapper = new UserWrapper();
        userWrapper.setUserName(UserUtils.getUserName());
        Page<UserAmountVo> page = userAmountService.getAmount(current, limit, userWrapper);
        List<UserAmountVo> records = page.getRecords();
        List<UserAmountVo> userAmountVos = UserAmountStatus.intToString(records);

        return Result.ok()
                .message("查询成功")
                .data("total", page.getTotal())
                .data("rows", userAmountVos);
    }

    //分页条件查询
    @AuthorityName(authorityId = 62, authorityName = "管理员 分页查询用户账户流水信息")
    @ApiOperation("admin 分页查询账户流水信息")
    @PostMapping(value = {"admin/getAmount/{current}/{limit}", "admin/getAmount"})
    public Result adminGetAmount(@PathVariable(value = "current", required = false) Integer current,
                            @PathVariable(value = "limit", required = false) Integer limit,
                            @RequestBody(required = false) UserWrapper userWrapper) {
        if (current == null || limit == null) {
            current = 0;
            limit = 10;
        }
        if (userWrapper == null) {
            userWrapper = new UserWrapper();
        }
        Page<UserAmountVo> page = userAmountService.getAmount(current, limit, userWrapper);
        // 状态的转化
        List<UserAmountVo> records = page.getRecords();
        List<UserAmountVo> userAmountVos = UserAmountStatus.intToString(records);

        return Result.ok()
                .message("查询成功")
                .data("total", page.getTotal())
                .data("rows", userAmountVos);
    }

}

